package com.bincloud.vue.admin.web.controller.api;

import com.alibaba.fastjson.JSON;
import com.bincloud.vue.admin.common.model.dto.response.BaseResDTO;
import com.bincloud.vue.admin.common.util.ResponseUtils;
import com.bincloud.vue.admin.web.service.api.CaptchaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "验证码相关接口")
@RestController
@RequestMapping(value = "/api/captcha")
@Slf4j
public class CaptchaController {
    @Resource
    private CaptchaService captchaService;

    @ApiOperation(value = "获取图形验证码", produces = "application/json")
    @GetMapping(value = "/get")
    public BaseResDTO<String> get(@Validated @RequestParam(value = "username") String username) {
        log.info("获取图形验证码:用户{}", username);
        String resDTO = captchaService.captcha(username);
        return ResponseUtils.ok(resDTO, "获取成功");
    }

    @ApiOperation(value = "验证图形验证码", produces = "application/json")
    @GetMapping(value = "/validate")
    public BaseResDTO<Boolean> validate(@Validated @RequestParam(value = "username") String username, @RequestParam(value = "code") String code) {
        log.info("验证图形验证码:用户{},验证码{}", username, code);
        boolean resDTO = captchaService.validate(username, code);
        return ResponseUtils.ok(resDTO, "验证成功");
    }

    @ApiOperation(value = "获取二次验证码", produces = "application/json")
    @GetMapping(value = "/twoFactor")
    public BaseResDTO<Boolean> twoFactor(@Validated @RequestParam(value = "username") String username, @RequestParam(value = "token") String token) {
        log.info("获取二次验证码:用户{},凭证{}", username, token);
        boolean resDTO = captchaService.twoFactor(username, token);
        log.info("获取二次验证码,返参:{}", JSON.toJSONString(resDTO));
        return ResponseUtils.ok();
    }

}
